package com.open.pokergames.texas.utils;

import com.open.pokergames.conn.bean.Player;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

public class RoomTool {

    public static ArrayList<Player> reorderSeat(String loginAccount, ConcurrentHashMap<String, Player> players) {
        ArrayList<Player> tempPlayers = new ArrayList<>(players.values());

        Player playerLogin = null;
        for (Player player : tempPlayers) {
            if (loginAccount.equals(player.getAccount())) {
                // 检测当前登录人是不是玩家，是则赋值playerLogin
                playerLogin = player;
            }
        }

        if (playerLogin == null) {
            // 如果当前用户是游客则直接返回原数据
            ArrayList<Player> order1Players = new ArrayList<>();
            order1Players.add(tempPlayers.get(2));
            order1Players.add(tempPlayers.get(3));
            order1Players.add(tempPlayers.get(4));
            order1Players.add(tempPlayers.get(5));
            order1Players.add(tempPlayers.get(6));
            order1Players.add(tempPlayers.get(7));
            order1Players.add(tempPlayers.get(8));
            order1Players.add(tempPlayers.get(0));
            order1Players.add(tempPlayers.get(1));
            return order1Players;
        } else {
            ArrayList<Player> order2Players = new ArrayList<>();
            for (int i = 0; i < tempPlayers.size(); i++) {
                if (playerLogin.getSeatNum().equals(tempPlayers.get(i).getSeatNum())) {
                    order2Players.addAll(tempPlayers.subList(i, tempPlayers.size()));
                    order2Players.addAll(tempPlayers.subList(0, i));
                }
            }
            return order2Players;
        }
    }

    public static ArrayList<Player> reorderSeatFor5Cards(String loginAccount, ConcurrentHashMap<String, Player> players) {
        ArrayList<Player> tempPlayers = new ArrayList<>(players.values());

        Player playerLogin = null;
        for (Player player : tempPlayers) {
            if (loginAccount.equals(player.getAccount())) {
                // 检测当前登录人是不是玩家，是则赋值playerLogin
                playerLogin = player;
            }
        }

        if (playerLogin == null) {
            // 如果当前用户是游客则直接返回原数据
            ArrayList<Player> order1Players = new ArrayList<>();
            order1Players.add(tempPlayers.get(2));
            order1Players.add(tempPlayers.get(3));
            order1Players.add(tempPlayers.get(4));
            order1Players.add(tempPlayers.get(0));
            order1Players.add(tempPlayers.get(1));
            return order1Players;
        } else {
            ArrayList<Player> order2Players = new ArrayList<>();
            for (int i = 0; i < tempPlayers.size(); i++) {
                if (playerLogin.getSeatNum().equals(tempPlayers.get(i).getSeatNum())) {
                    order2Players.addAll(tempPlayers.subList(i, tempPlayers.size()));
                    order2Players.addAll(tempPlayers.subList(0, i));
                }
            }
            return order2Players;
        }
    }
}
